<?php
/**
 * 材料（大类）
 * User: bgl
 * Date: 2023/6/20
 * Time: 11:14
 */

namespace App\Admin\Controllers\Products;


use App\Admin\Extensions\Grid\Tool\Export;
use App\Admin\Extensions\Grid\Tool\Import;
use App\Models\Product\ProductMaterialApplicationClassify;
use App\Models\Product\ProductMaterialClassifyMax;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;

class MaterialClassifyMaxController extends AdminController{

    protected $title = '材料大类';

    protected function grid(){

        $grid = new Grid(new ProductMaterialClassifyMax());
        $grid->column('id', 'ID');
        $grid->column('title', '材料大类名称(中文)');
        $grid->column('code', '材料大类代码(缩写)');
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
            $actions->disableDelete();
        });
        $grid->tools(function (Grid\Tools $tools) {
            $tools->append(new Import('products/material-classify-upload?type=max'));
        });

        $grid->disableRowSelector();
        $grid->disableExport();
        $grid->filter(function($filter){
            $filter->column(1/3, function ($filter){
                $filter->where(function ($query) {
                    $query->where('code', 'like', "%{$this->input}%")
                        ->orWhere('title', 'like', "%{$this->input}%");
                }, '查询','search8')->placeholder('模糊查询材料名称，代码');
            });
        });

        return $grid;
    }

    public function form(){
        $form = new Form(new ProductMaterialClassifyMax());

        $form->hidden('id');
        $form->text('title', '材料大类名称(中文)')->rules('required')
            ->creationRules(['required', "unique:product_material_classify_max"])
            ->updateRules(['required', "unique:product_material_classify_max,title,{{id}}"])
            ->help('输入材料名称，例：面料');

        $form->text('code', '材料大类代码(缩写)')->pattern('[A-Z]{1}')->rules('required')
            ->creationRules(['required', "unique:product_material_classify_max"])
            ->updateRules(['required', "unique:product_material_classify_max,code,{{id}}"])
            ->help('材料英文一位大写字母，例：面料(Fabric），可标识为：F');

        $form->tools(function (Form\Tools $tools) {
            $tools->disableView();
            $tools->disableDelete();
        });

        return $form;
    }

}
